package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.annotations.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployDTO;
import com.sky.dto.EmployLoginDTO;
import com.sky.entity.EmployeeEntity;
import com.sky.enumeration.EmployeeStatusEnum;
import com.sky.enumeration.OperationType;
import com.sky.exceptionHandler.ServiceException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.TableDataInfo;
import com.sky.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public EmployeeEntity login(EmployLoginDTO loginDTO) {
        String userName = loginDTO.getUserName();
        String password = loginDTO.getPassword();

        EmployeeEntity employee = employeeMapper.getByUserName(userName);
//        log.info(String.valueOf(employee));
//        String string = JSON.toJSONString(employee);
//        log.info("序列化，{}",string);
//        EmployeeEntity employeeEntity = JSON.parseObject(string, EmployeeEntity.class);
//        log.info("反序列化,{}",employeeEntity);

        if (employee == null) {
            log.error("数据库找不到对应的员工");
            throw new ServiceException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        if (!password.equals(employee.getPassword())) {
            log.error("密码错误");
            throw new ServiceException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus().equals(EmployeeStatusEnum.Disable)) {
            log.error("该员工被禁用了");
            throw new ServiceException(MessageConstant.ACCOUNT_LOCKED);
        }

        return employee;
    }

    @Override
    public void save(EmployDTO dto) {
        String username = dto.getUsername();
        EmployeeEntity employee = employeeMapper.getByUserName(username);
        if (employee != null) {
            throw new ServiceException("用户名已存在");
        }
        EmployeeEntity employeeEntity = new EmployeeEntity();
        // 将 dto 中的属性合并到 employeeEntity，前提需保证双方存在相同的属性
        BeanUtils.copyProperties(dto, employeeEntity);
        employeeEntity.setPassword(PasswordConstant.DEFAULT_PASSWORD);
        employeeEntity.setStatus(EmployeeStatusEnum.Enable);
        // 已经添加了AOP，不需要下面这些
//        LocalDateTime now = LocalDateTime.now();
//        employeeEntity.setCreateTime(now);
//        employeeEntity.setUpdateTime(now);
//
//        employeeEntity.setCreateUser(BaseContext.getId());
//        employeeEntity.setUpdateUser(BaseContext.getId());

        employeeMapper.save(employeeEntity);
    }

    @Override
    public List<EmployeeEntity> page(String name, Integer page, Integer size) {
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 10;
        }
        PageHelper.startPage(page, size);
        List<EmployeeEntity> list = employeeMapper.getByName(name);
        PageInfo<EmployeeEntity> pageInfo = new PageInfo<>(list);
        return pageInfo.getList();
    }

    @Override
    public void enableOrDisable(EmployeeStatusEnum statusEnum, Long id) {
        EmployeeEntity employeeEntity = EmployeeEntity.builder().status(statusEnum).id(id).build();
        log.info("更新员工状态信息，{}", employeeEntity);
        employeeMapper.update(employeeEntity);
    }

    @Override
    public EmployeeEntity getById(Long id) {
        EmployeeEntity employeeEntity = employeeMapper.getById(id);
        return employeeEntity;
    }

    @Override
    public void update(EmployDTO employDTO) {
        EmployeeEntity employeeEntity = new EmployeeEntity();
        BeanUtils.copyProperties(employDTO,employeeEntity);
//        employeeEntity.setUpdateUser(BaseContext.getId());
//        employeeEntity.setUpdateTime(LocalDateTime.now());
        employeeMapper.update(employeeEntity);
    }
}
